<?php
namespace app\admin\service;

use bxkj_common\service\Sevrice;

class Novel extends Sevrice
{
    public function getList(array $get = [], $field = '*', $sort = 'id desc')
    {
        $data = [];
        $data['list'] = $this->where($get)->field($field)->order($sort)->select()->toArray();
        $data['count'] = $this->where($get)->field($field)->order($sort)->count();
        return $data;
    }

    public function add($params)
    {
        $validate = new \app\admin\validate\Novel();
        $result = $validate->check($params);
        if (!$result) return $this->setError($validate->getError());
        $id = $this->save($params);
        if (!$id) return $this->setError('新增失败');
        return true;
    }

    public function detail($id)
    {
        $detail = $this->where(["id" => $id])->find();
        if (!$detail) return $this->setError('信息获取失败');
        return $detail;
    }

    public function edit($params, $where): bool
    {
        unset($params['id']);
        $validate = new \app\admin\validate\Category();
        $result = $validate->check($params);
        if (!$result) return $this->setError($validate->getError());
        $id = $this->update($params, $where);
        if (!$id) return $this->setError('更新失败');
        return true;
    }

    public function del($ids): bool
    {
        $ids = array_unique(is_array($ids) ? $ids : explode(',', $ids));
        $num = $this->whereIn('id', $ids)->delete();
        if (!$num) return $this->setError('删除失败');
        return true;
    }

}